<template>
  <div>
    <div v-show="articelStatus == false">
      <el-table :data="tableData" border style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column prop="name" label="序号" width="60">
          <template slot-scope="scope">{{scope.$index+1}}</template>
        </el-table-column>
        <el-table-column prop="name" label="发布时间" width="100">
          <template slot-scope="scope">{{timeFun(scope.row.createtime)}}</template>
        </el-table-column>
        <el-table-column prop="articleThum" label="封面" width="90">
          <template slot-scope="scope">
            <img :src="scope.row.articleThum" style="width:70px;height:80px"/>
          </template>
        </el-table-column>
        <el-table-column prop="articleTitle" label="标题"> </el-table-column>      
        <el-table-column prop="type" label="分类" >
          <template slot-scope="scope">
            {{typeFun(scope.row.type)}}
          </template>
        </el-table-column>      
        <el-table-column prop="articleUser.userNick" label="发布者ID" width="100"> </el-table-column>      
        <el-table-column prop="countComment" label="评论" width="50" > </el-table-column>      
        <el-table-column prop="countLike" label="喜欢" width="60"> </el-table-column>      
        <el-table-column prop="countRead" label="点击量" width="70">> </el-table-column>      
        <el-table-column prop="status" label="状态" width="50">
          <template slot-scope="scope">
            
            <span style="color:#67c23a" v-if="scope.row.articleStatus==1">上线</span>
            <span v-if="scope.row.articleStatus==2">下线</span>
            <span style="color:#fd0767" v-if="scope.row.isRecommend == 1">推荐</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope" >
            <div>
              <el-button class="addBtn " @click="untickFun(scope.row)" v-if="scope.row.orderAt && userRole.AC_501004" type="warning"> 取消置顶</el-button>
              <el-button class="addBtn" @click="tickFun(scope.row)" v-else> 置顶</el-button>
              <el-button class="addBtn" @click="editFun(scope.row)" v-if="userRole.AC_501002 && userRole.CM_601001"> 编辑</el-button>
              <el-button  v-if="scope.row.articleStatus==1 && userRole.AC_501003" type="danger"  class="addBtn" @click="deletData(scope.row.articleId,2)"> 下线</el-button>
              <el-button  v-if="scope.row.articleStatus==2 && userRole.AC_501003" type="danger"  class="addBtn" @click="deletData(scope.row.articleId,1)"> 上线</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
        <span>这是一段信息</span>
        <span slot="footer" class="dialog-footer">
          <el-button  @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>
     
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  import Http from '@/Http/Http'
  import getTime from '@/Http/getTime'

  export default {
    data() {
      return {
        userRole:JSON.parse(sessionStorage.getItem('userSolt')),
        articelStatus:false,
        dataArtice:'',
        currentPage4: 1,//分野  
        sheachValue:'',
        total:0,      
        dialogVisible:false,
        sortStatus:false,
        tuijians:'',
        seachtype:0,
        value:'全部',
        options:[{
           value:0,
          label:'全部'
        },{
          value:1,
          label:'牙齿矫正'
        },{
          value:2,
          label:'种植牙'
        },{
          value:3,
          label:'口腔健康'
        },{
          value:4,
          label:'儿童口腔'
        }],
        tableData: [],
        page:1,
        pageSize:10
      }
    },
    props:['navStatus'],
    created() {
      this.getlist()
    },
    
    methods: {
      getlist() {
        
        const parm ={
          "pageNo":this.page,
          "pageSize":this.pageSize,
          "title":this.sheachValue,
          "type":this.seachtype
        };
        
        if(this.navStatus == 0){
          parm.status = 1;
        } else if(this.navStatus == 1) {
           parm.status = 2;
        } else {
          parm.status ='';
        }

        Http.getArticelList(parm)
        .then( res => {
          this.tableData = res.data.data.rows;
          this.total = res.data.data.total;
           this.dataArtice = '';
          this.articelStatus = false
          
        })
        .catch( error => {
          
        })
      },  
      typeFun(row){
        return this.options[row].label
      },
      timeFun(val) {
        return getTime(val)
      },
      goEdit(){
        this.articelStatus = true;
      },
      tickFun(row) {
        Http.postStickAritice(row.articleId)
        .then( res => {
          if( res.data.code == 200 ){
            this.$message.success('置顶成功')
            this.getlist()
          } else if(res.data.code == -1003) {
            this.$message.error(`请求失败原因：${res.data.message}`)
             setTimeout(()=>{
              this.$router.push('/')
            },2000)
          }
       })
       .catch( res => {
         this.$message.error('服务器请求失败')
       })
      },
      untickFun(row) {
        Http.unPostStickAritice(row.articleId)
        .then( res => {
          if( res.data.code == 200 ){
            this.$message.success('取消置顶成功')
            this.getlist()
          } else if(res.data.code == -1003) {
            this.$message.error(`请求失败原因：${res.data.message}`)
             setTimeout(()=>{
              this.$router.push('/')
            },2000)
          }
       })
       .catch( res => {
         this.$message.error('服务器请求失败')
       })
      },
      
      editFun(val) {
        this.$emit('goEdit',val)
      },
      deletData(row,val) {
        let txt='';
        if(val==1){
          txt = '上线'
        } else {
          txt = '下线'
        }
        
        this.$confirm(`确认${txt}此文章吗 ？`)
        .then(_ => {
          Http.upArticle(row,val)
          .then( res => {
            
            if( res.data.code == 200 ){
              this.$message.success('修改成功')
              this.getlist()         
            } else if(res.data.code == -1003) {
              this.$message.error(`请求失败原因：${res.data.message}`)
              this.$router.push('/')
            }
          })
          .catch( error => {
            this.$message.error('服务器请求失败')
          })
        })
        .catch(_ => {
          //取消部分
          
        });
      },
      //选择 推荐
      handleSelectionChange(val) {
        this.tuijians = val;
      },
      seachData(val) {
        this.sheachValue = val.title;
        this.seachtype = val.type;
        this.getlist();
      },
     
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
     
      //排序
      sortBtn() {
        this.sortStatus = true
      },
      sorlCheckStatus(){
        this.sortStatus = false
      },
       //分页
      handleSizeChange(val) {
        this.pageSize = val;
        this.getlist()
        
      },
      handleCurrentChange(val) {
        this.page = val;
        this.getlist()
        
      },
     
    }
  }
</script>